#include "SQLDropViewStatement.h"
#include "SQLExprTableSource.h"

#include "SQLDDLStatement.h"
#include "../../visitor/SQLASTVisitor.h"
#include "../SQLName.h"
#include "../../../DbType.h"

SQLDropViewStatement::SQLDropViewStatement()
{
  tableSources = std::make_shared<std::list<SQLExprTableSource_ptr>>();
}

SQLDropViewStatement::SQLDropViewStatement(DbType_ptr  dbType)
    : SQLStatementImpl(dbType)
{
  tableSources = std::make_shared<std::list<SQLExprTableSource_ptr>>();
}

SQLDropViewStatement::SQLDropViewStatement(SQLName_ptr name)
    : SQLDropViewStatement(SQLExprTableSource_ptr(new SQLExprTableSource(name)))
{
}

SQLDropViewStatement::SQLDropViewStatement(SQLExprTableSource_ptr tableSource)
{
  tableSources = std::make_shared<std::list<SQLExprTableSource_ptr>>();
  this->tableSources->push_back(tableSource);
}

SQLExprTableSource_list_ptr SQLDropViewStatement::getTableSources()
{
  return tableSources;
}

void SQLDropViewStatement::addPartition(SQLExprTableSource_ptr tableSource)
{
  if (tableSource != NULL)
  {
    tableSource->setParent(SharedObject(SQLDropViewStatement));
  }
  this->tableSources->push_back(tableSource);
}

void SQLDropViewStatement::setName(SQLName_ptr name)
{
  this->addTableSource(SQLExprTableSource_ptr(new SQLExprTableSource(name)));
}

void SQLDropViewStatement::addTableSource(SQLName_ptr name)
{
  this->addTableSource(SQLExprTableSource_ptr(new SQLExprTableSource(name)));
}

void SQLDropViewStatement::addTableSource(SQLExprTableSource_ptr tableSource)
{
  tableSources->push_back(tableSource);
}

BOOL_ptr SQLDropViewStatement::isCascade()
{
  return cascade;
}

void SQLDropViewStatement::setCascade(BOOL_ptr cascade)
{
  this->cascade = cascade;
}

void SQLDropViewStatement::accept0(SQLASTVisitor_ptr visitor)
{
  if (visitor->visit(SharedObject(SQLDropViewStatement)))
  {
    auto it_tableSources = tableSources->begin();
    for (int i = 0; i < tableSources->size(); i++)
    {
      std::advance(it_tableSources, i);
      SQLExprTableSource_ptr item = *it_tableSources;
      if (item != NULL)
      {
        item->accept(visitor);
      }
    }
  }
  visitor->endVisit(SharedObject(SQLDropViewStatement));
}

BOOL_ptr SQLDropViewStatement::isRestrict()
{
  return restrict;
}

void SQLDropViewStatement::setRestrict(BOOL_ptr restrict)
{
  this->restrict = restrict;
}

BOOL_ptr SQLDropViewStatement::isIfExists()
{
  return ifExists;
}

void SQLDropViewStatement::setIfExists(BOOL_ptr ifExists)
{
  this->ifExists = ifExists;
}

SQLExprTableSource_list_ptr SQLDropViewStatement::getChildren()
{
  return tableSources;
}

SQLDDLStatement::DDLObjectType SQLDropViewStatement::getDDLObjectType()
{
  return SQLDDLStatement::DDLObjectType::VIEW;
}
